﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DisplayBoardInfo.Server.Entities
{
    class Appointments
    {
        #region Factory methods

        /// <summary>
        /// Gets all appointments.
        /// </summary>
        /// <returns>Collection of all appointments.</returns>
        public static IEnumerable<Appointment> GetAll()
        {
            List<Appointment> result = new List<Appointment>();
            Model.DatabaseContextDataContext db = new Model.DatabaseContextDataContext();
            foreach (Model.Appointment item in db.Appointments)
            {
                result.Add(new Appointment(item.ID));
            }

            return result;
        }

        /// <summary>
        /// Gets all appointments.
        /// </summary>
        /// <returns>Collection of all appointments.</returns>
        public static IEnumerable<Appointment> GetByDate(DateTime date)
        {
            List<Appointment> result = new List<Appointment>();
            Model.DatabaseContextDataContext db = new Model.DatabaseContextDataContext();
            foreach (Model.Appointment item in db.Appointments.Where(i=>i.DateTime.Year == date.Year && i.DateTime.DayOfYear == date.DayOfYear))
            {
                result.Add(new Appointment(item.ID));
            }

            return result;
        }

        /// <summary>
        /// Gets appointment with spcyfic ID.
        /// </summary>
        /// <param name="ID">ID of appointment</param>
        /// <returns>Single instance of appointment with specyfic ID.</returns>
        /// <exception cref="IndexOutOfRangeException"></exception>
        public static Appointment GetByID(int ID)
        {
            if (IsAppointmentExist(ID)) return new Appointment(ID);
            else throw new IndexOutOfRangeException("Appointment is not exist.");
        }

        public static IEnumerable<Appointment> GetByDoctor(string username)
        {
            List<Appointment> result = new List<Appointment>();
            Model.DatabaseContextDataContext db = new Model.DatabaseContextDataContext();
            foreach (Model.Appointment item in db.Appointments.Where(i => i.Doctor == username))
            {
                result.Add(new Appointment(item.ID));
            }

            return result;
        }

        #endregion

        #region Helper methods

        /// <summary>
        /// Checks that appointment exist.
        /// </summary>
        /// <param name="ID">ID of appointment.</param>
        /// <returns>True if appointment exist or false otherwise.</returns>
        public static bool IsAppointmentExist(int ID)
        {
            Model.DatabaseContextDataContext db = new Model.DatabaseContextDataContext();
            return db.Appointments.Where(i => i.ID == ID).Count() > 0;
        }

        #endregion
    }
}
